package com.online_vote_system.demo.controller;

import com.online_vote_system.demo.service.PlayerService;
import com.online_vote_system.demo.vo.Player;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.awt.*;
import java.util.List;

@Controller
public class PlayerController {

    @Autowired
    PlayerService playerService;

    @GetMapping("/PlayerOs")
    public String PlayerOs(Model model){
        List<Player> list = playerService.list();
        model.addAttribute("players",list);
        return "html/Admin/playerIndex";
    }

    @GetMapping("/delPlayer/{playerId}")
    public String delPlayer(@PathVariable(value = "playerId") String playerId,
                            Model model){
        playerService.removeById(playerId);
        return PlayerOs(model);
    }

    @GetMapping("/toPlayerInsert")
    public String toPlayerInsert(){
        return "html/Admin/playerInsert";
    }

    @PostMapping("/insPlayer")
    public String insPlayer(String playerName,
                            Model model){
        Player player = new Player();
        player.setPlayerName(playerName);
        playerService.save(player);
        return PlayerOs(model);
    }
    @GetMapping("/changePlayer/{playerId}")
    public String changePlayer(@PathVariable(value = "playerId") String playerId,
                               @RequestParam(value = "playerName") String playerName,
                               Model model){
        Player player = new Player();
        player.setPlayerId(Integer.valueOf(playerId));
        player.setPlayerName(playerName);
        model.addAttribute("player",player);
        return "html/Admin/playerChange";
    }

    @PostMapping("/updatePlayer")
    public String updatePlayer(String playerId,
                               String playerName,
                               Model model){
        Player player = new Player();
        player.setPlayerId(Integer.valueOf(playerId));
        player.setPlayerName(playerName);
        playerService.updateById(player);
        return PlayerOs(model);
    }


}
